18. 练习 — 将列表赋值给变量

练习 — 将列表赋值给变量

在这节课的下一部分,我们将学习一些更复杂的 for 循环。这些循环可能有点复杂,确保在继续之前,先巩固下一些核心概念。

首先,你已经知道我们可以接受列表并将其赋值给变量,如下所示:

some_list = [1, 2, 3, 4]

如果我们想在某个地方使用该列表,我们可以直接输入名称 some_list ,代码就像我们在该位置放入该列表一样运行。

例如,在下面的 workspace 中,你将看到有一个边很多的图形。要绘制该图形,我们将使用一个有很多项目的列表。目前,代码直接在循环里使用列表。我们将其更改为首先将该列表赋值给一个变量。

Task List:

Task Feedback:

很棒!

Workspace

This section contains either a workspace (it can be a Jupyter Notebook workspace or an online code editor work space, etc.) and it cannot be automatically downloaded to be generated here. Please access the classroom with your account and manually download the workspace to your local machine. Note that for some courses, Udacity upload the workspace files onto https://github.com/udacity , so you may be able to download them there.

Workspace Information:

  • Default file path:
  • Workspace type: html-live
  • Opened files (when workspace is loaded): n/a

备注 :如果你无法打开上面的workspace,请去 这里


## ⚠️ 剧透! **下面是我们的解决方案。**如果你能认真完成练习,然后再将你的代码与我们的代码进行对比,学习效果将更好!

----

解决方案

import turtle
amy = turtle.Turtle()
amy.color("cyan")

some_list = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

for item in some_list:
    amy.forward(50)
    amy.right(30)